package com.controller.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.sound.sampled.LineUnavailableException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.DateUtils;
import com.common.EasyuiUtils;
import com.dao.task.TaskMapper;
import com.model.sys.SysUser;

@Controller
public class StatisControllser {
	static final Logger logger = LoggerFactory.getLogger(StatisControllser.class);
	@Autowired
	private TaskMapper taskMapper;

	@RequestMapping(value = "/statis/selectStatistask", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String selectStatistask(HttpServletRequest request) {
		try {
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysuser");
			HashMap<String, Object> hashtable = new HashMap<>();
			int week = DateUtils.getWeek(new Date());
			String year = DateUtils.getCurrentYear();
			hashtable.put("week", year + week);
			hashtable.put("creatorid", sysUser.getUserid());
			hashtable.put("superadmin", sysUser.getSuperadmin());
			int page = Integer.parseInt(request.getParameter("page"));
			int size = Integer.parseInt(request.getParameter("rows"));
			hashtable.put("PAGENUM", (page - 1) * size);
			hashtable.put("PAGESIZE", size);
			String flag = request.getParameter("flag");
			hashtable.put("flag", flag);
			List<HashMap<String, Object>> list = taskMapper.selectStatistask(hashtable);
			String jsonString = JSON.toJSONString(list);
			jsonString = EasyuiUtils.getDatagridPageJson(jsonString, taskMapper.selectStatistaskCount(hashtable));
			logger.info("selectStatistask:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("selectStatistask:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/statis/getRate", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String getRate(HttpServletRequest request) {
		try {
			SysUser sysUser = (SysUser) request.getSession().getAttribute("sysuser");
			HashMap<String, Object> hashtable = new HashMap<>();
			hashtable.put("creatorid", sysUser.getUserid());
			List<HashMap<String, Object>> list = taskMapper.getRate(hashtable);
			String jsonString = getChart(list);
			logger.info("getRate:{}", jsonString);
			return jsonString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("getRate:{}", e.getMessage());
			return e.getMessage();
		}
	}

	private String getChart(List<HashMap<String, Object>> list) {
		String result = "";
		if (list.size() == 0) {
			result = "{\"lable\":[],\"value\":[]}";
			return result;
		}
		String label = "[\"", value = "[";
		result = "{\"lable\":%s,\"value\":%s}";
		for (HashMap<String, Object> hashMap : list) {
			label += hashMap.get("name") + "\",\"";
			value += hashMap.get("rate") + ",";
		}
		label = label.substring(0, label.length() - 2);
		label += "]";
		value = value.substring(0, value.length() - 1);
		value += "]";
		result = String.format(result.toString(), label, value);
		return result.toString();
	}
}
